{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "override USE_NET <class 'src.networks.XintongNetwork'>\n",
      "override DATASET_PROC_METHOD_TRAIN Random\n",
      "override DATASET_PROC_METHOD_VAL Rescale\n",
      "override MAX_CATEGORY_NUM 1\n",
      "override IMAGE_EMBED_SIZE 512\n",
      "override WEIGHT_IMAGE_TEXT 1.0\n",
      "override F_WEIGHT_SPARSE_SOFTMAX 1.0\n",
      "override B_WEIGHT_SPARSE_SOFTMAX 1.0\n",
      "override USE_PRETRAINED_WORD_EMBEDDING True\n",
      "override WORD_EMBED_SIZE 300\n",
      "override MAX_VOCAB_SIZE 2500\n",
      "override OUTFIT_NAME_PAD_NUM 10\n",
      "override NUM_EPOCH 20\n",
      "override LEARNING_RATE 0.2\n",
      "override LEARNING_RATE_DECAY 0.5\n",
      "override LEARNING_RATE_DECAY_EVERY_EPOCHS 2\n",
      "override GRADIENT_CLIP 5\n",
      "override BATCH_SIZE 10\n",
      "override SAVE_EVERY_STEPS 10000\n",
      "override SAVE_EVERY_EPOCHS 1\n",
      "override VAL_WHILE_TRAIN True\n",
      "override VAL_FASHION_COMP_FILE fashion_compatibility_small.txt\n",
      "override VAL_FITB_FILE fill_in_blank_test_small.json\n",
      "override VAL_BATCH_SIZE 8\n",
      "override VAL_EVERY_STEPS 500\n",
      "override VAL_EVERY_EPOCHS 1\n",
      "override VAL_START_EPOCH 1\n",
      "override device cuda:0\n",
      "override TRAIN_DIR runs/src.conf.xintong/11-13 22:32:06\n",
      "override VAL_DIR runs/src.conf.xintong/11-13 22:32:06\n",
      "override MODEL_NAME src.conf.xintong\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.utils.data\n",
    "from src.const import base_path\n",
    "import numpy as np\n",
    "import cv2\n",
    "from torchvision import transforms\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "from skimage import io, transform\n",
    "import skimage\n",
    "from src import const\n",
    "import json\n",
    "import os\n",
    "import nltk\n",
    "from src.utils import load_json, build_vocab, Vocab\n",
    "from src.dataset import *\n",
    "from src.base_networks import *\n",
    "from src.networks import *\n",
    "from torch import nn\n",
    "import torchvision\n",
    "from torch.nn import functional as F\n",
    "from src.utils import merge_const\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from src.utils import FITBBenchMarkHelper\n",
    "merge_const('src.conf.xintong')\n",
    "const.BATCH_SIZE = 3\n",
    "const.device = 'cpu'\n",
    "class _(object):\n",
    "    pass\n",
    "self = _()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_set = load_json(os.path.join(const.base_path, 'train_no_dup.json'))\n",
    "vocab = build_vocab(train_set)\n",
    "train_dataset = PolyvoreDataset(train_set, const.DATASET_PROC_METHOD_TRAIN, vocab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_js = []\n",
    "for i in range(300):\n",
    "    outfit = train_dataset.js[i]\n",
    "    set_id = outfit['set_id']\n",
    "    num_item = len(outfit['items'])\n",
    "    question = []\n",
    "    blank_pos = np.random.randint(1, num_item + 1)\n",
    "    for j in range(num_item):\n",
    "        if j + 1 == blank_pos:\n",
    "            pass\n",
    "        else:\n",
    "            question.append('{}_{}'.format(set_id, outfit['items'][j]['index']))\n",
    "    \n",
    "    answers = []\n",
    "    answers.append('{}_{}'.format(set_id, outfit['items'][blank_pos - 1]['index']))\n",
    "    for j in range(3):\n",
    "        rnd = np.random.randint(0, len(train_dataset.idx2im))\n",
    "        answers.append(train_dataset.idx2im[rnd])\n",
    "    new_js.append({\n",
    "        'question': question,\n",
    "        'answers': answers,\n",
    "        \"blank_position\": blank_pos,\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(const.base_path + './train_fitb_test.json', 'w') as f:\n",
    "    json.dump(new_js, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
